package com.lzxuni.modules.pestdetails.controller;


import com.alibaba.fastjson.JSON;
import com.lzxuni.common.utils.FileUtil;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.UuidUtil;
import com.lzxuni.common.utils.date.DateUtil;
import com.lzxuni.modules.basics.entity.BasicTree;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.FileEntity;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.service.FileEntityService;
import com.lzxuni.modules.pestdetails.entity.PestDisastermanagement;
import com.lzxuni.modules.pestdetails.service.PestDisastermanagementService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * <p>
 * 虫害信息管理 前端控制器
 * </p>
 *
 * @author rdf
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/LR_PestDisastermanagement/PestDisastermanagement")
public class PestDisastermanagementController extends BaseController{

    @Autowired
    private PestDisastermanagementService pestDisastermanagementService;
    @Autowired
    private FileEntityService fileEntityService;
    private static String typeName = "虫情灾害图片";
    private static String ywType = "pestdis_img";

    //主页面
    @RequestMapping("/Index")
    public ModelAndView Index() {
        ModelAndView mv = new ModelAndView("/LR_Pest/pestdisastermanagement/index");
        return mv;
    }
    //查询数据
    @RequestMapping("/GetList")
    public Object GetList(String pagination,String queryJson){
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        PestDisastermanagement pestDisastermanagement = JSON.parseObject(queryJson, PestDisastermanagement.class);
        PageData pageData = getPageData(pestDisastermanagementService.queryPage(pageParameter, pestDisastermanagement));
        return R.ok().put("data", pageData);
    }
    //表单页
    @RequestMapping("/Form")
    public ModelAndView Form(String keyValue,String queryJson) throws Exception{
        ModelAndView mv = new ModelAndView("/LR_Pest/pestdisastermanagement/form");
        BasicTree basicTree = JSON.parseObject(queryJson, BasicTree.class);
        mv.addObject("basicTree", basicTree);
        FileEntity fileBeanCustom = new FileEntity();
        fileBeanCustom.setYwId(keyValue);
        fileBeanCustom.setYwType(ywType);
        mv.addObject("imgCount", fileEntityService.queryNumByFileEntity(fileBeanCustom));
        mv.addObject("id", keyValue);
        return mv;
    }

    //保存数据
    @RequestMapping("/SaveForm")
    public Object insertDo(String keyValue, PestDisastermanagement pestDisastermanagement, String pestdis_imgs) throws Exception{
        if(StringUtils.isEmpty(keyValue)){
            String ywId = UuidUtil.get32UUID();
            if (!com.lzxuni.common.utils.StringUtils.isEmpty(pestdis_imgs) && !"&amp;nbsp;".equals(pestdis_imgs)) {
                fileEntityService.insert(pestdis_imgs.replace("&quot;", "\""), ywId, typeName, ywType, null);
            }

            pestDisastermanagement.setDisasterId(ywId);
            pestDisastermanagement.setCreateTime(new Date());
            pestDisastermanagementService.save(pestDisastermanagement);
            return R.ok("保存成功");
        }else{
            pestDisastermanagement.setDisasterId(keyValue);
            fileEntityService.deleteByYwId(keyValue);
            if (!StringUtils.isEmpty(pestdis_imgs) && !"&amp;nbsp;".equals(pestdis_imgs)) {
                fileEntityService.insert(pestdis_imgs.replace("&quot;", "\""), keyValue, typeName, ywType, null);
            }
            pestDisastermanagementService.updateById(pestDisastermanagement);
            return R.ok("修改成功");
        }
    }
    //删除数据
    @RequestMapping("/DeleteForm")
    public Object delete(String keyValue) {
        fileEntityService.deleteByYwId(keyValue);
        pestDisastermanagementService.removeById(keyValue);
        return R.ok("删除成功");
    }
    @RequestMapping("/Export")
    public void exportExcel(HttpServletResponse response, String queryJson, HttpServletRequest request) throws IOException {
        PestDisastermanagement pestDisastermanagement = JSON.parseObject(queryJson, PestDisastermanagement.class);
        //System.out.println(diseaseManagement.toString());
        BufferedOutputStream fos = new BufferedOutputStream(response.getOutputStream());
        String filename = "虫情灾害信息" + DateUtil.DateToString(new Date(), "yyyyMMddHHmm") + ".xls";
        String agent = request.getHeader("User-Agent");
        filename = FileUtil.encodeDownloadFilename(filename, agent);
        String contentType = request.getSession().getServletContext().getMimeType(filename);
        response.setContentType(contentType);
        response.setHeader("content-disposition", "attchment;filename=" + filename);
        Workbook wb = null;
        try {
            wb = pestDisastermanagementService.exportExcel(pestDisastermanagement);
            wb.write(fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            fos.close();
        }
    }

}

